package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.PaperSubject;
import com.ruoyi.system.domain.vo.DistributePaperVo;
import com.ruoyi.system.service.IPaperSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PaperMapper;
import com.ruoyi.system.domain.Paper;
import com.ruoyi.system.service.IPaperService;

/**
 * 试卷Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-19
 */
@Service
public class PaperServiceImpl implements IPaperService
{
    @Autowired
    private PaperMapper paperMapper;

    @Autowired
    private IPaperSubjectService paperSubjectService;

    /**
     * 查询试卷
     *
     * @param paperId 试卷主键
     * @return 试卷
     */
    @Override
    public Paper selectPaperByPaperId(Long paperId)
    {
        return paperMapper.selectPaperByPaperId(paperId);
    }

    /**
     * 查询试卷列表
     *
     * @param paper 试卷
     * @return 试卷
     */
    @Override
    public List<Paper> selectPaperList(Paper paper)
    {
        return paperMapper.selectPaperList(paper);
    }

    /**
     * 分配试卷
     * @return 结果
     */
    @Override
    public int distributePaper(DistributePaperVo distributePaperVo)
    {
        Paper paper1 = paperMapper.selectPaperByPaperId(distributePaperVo.getPaperId());
        if (paper1==null){
            return 0;
        }
        Paper paper=new Paper();
        Date date=new Date();
        paper.setMakeTime(date);
        paper.setCount((long) distributePaperVo.getSubjectList().size());
        paper.setPaperId(distributePaperVo.getPaperId());
        paper.setName(distributePaperVo.getName());
//      给试卷分配考题
        List<PaperSubject> subjectList = paperSubjectService.selectPaperSubjectByPaperId(distributePaperVo.getPaperId());
        if(subjectList.size()==0){
            for(int i = 0; i<distributePaperVo.getSubjectList().size(); i++){
                PaperSubject paperSubject=new PaperSubject();
                paperSubject.setPaperId(distributePaperVo.getPaperId());
                paperSubject.setSubjectId(Long.valueOf(distributePaperVo.getSubjectList().get(i)));
                paperSubject.setSubjectNow(String.valueOf(i));
                paperSubjectService.insertPaperSubject(paperSubject);
            }
        }else{
            paperSubjectService.deletePaperSubjectByPaperIds(new Long[]{distributePaperVo.getPaperId()});
            for(int i = 0; i<distributePaperVo.getSubjectList().size(); i++){
                PaperSubject paperSubject=new PaperSubject();
                paperSubject.setPaperId(distributePaperVo.getPaperId());
                paperSubject.setSubjectId(Long.valueOf(distributePaperVo.getSubjectList().get(i)));
                paperSubject.setSubjectNow(String.valueOf(i));
                paperSubjectService.insertPaperSubject(paperSubject);
            }
        }
        return paperMapper.updatePaper(paper);
    }






    /**
     * 新增试卷
     *
     * @param paper 试卷
     * @return 结果
     */
    @Override
    public int insertPaper(Paper paper)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Date date=new Date();
        paper.setMakeTime(date);
        paper.setCount(0L);
        paper.setProducer(loginUser.getUsername());
        return paperMapper.insertPaper(paper);
    }

    /**
     * 修改试卷
     *
     * @param paper 试卷
     * @return 结果
     */
    @Override
    public int updatePaper(Paper paper)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Date date=new Date();
        paper.setMakeTime(date);
        paper.setProducer(loginUser.getUsername());
        return paperMapper.updatePaper(paper);
    }

    /**
     * 批量删除试卷
     *
     * @param paperIds 需要删除的试卷主键
     * @return 结果
     */
    @Override
    public int deletePaperByPaperIds(Long[] paperIds)
    {
        return paperMapper.deletePaperByPaperIds(paperIds);
    }

    /**
     * 删除试卷信息
     *
     * @param paperId 试卷主键
     * @return 结果
     */
    @Override
    public int deletePaperByPaperId(Long paperId)
    {
        return paperMapper.deletePaperByPaperId(paperId);
    }
}
